<?php

namespace App\Jobs;
use App\Models\Circle;
use App\Models\Subscribe;
use App\Services\PublishTemplateService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class PublishLotteryStartJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $circle_id;

    /**
     * Create a new job instance.
     * PublishLotteryJob constructor.
     * @param $circle_id
     */
    public function __construct($circle_id)
    {
        $this->circle_id = $circle_id;
    }

    /**
     * Execute the job.
     * @link https://shimo.im/mindmaps/y6ydprxxxJKCggPx/
     * @return void
     */
    public function handle()
    {
        $activity_item = Circle::find($this->circle_id);
        if (!$activity_item)
            return;

        Subscribe::where('type', Subscribe::TYPE_ACTIVITY_START)->withCertain('user', ['openid'])->chunkById(50, function ($list) use($activity_item) {
            foreach ($list as $item) {
                $service = new PublishTemplateService();
                $service->activity_start($item->user->openid, $activity_item->title, $activity_item->id,$activity_item->activity_start_at, $activity_item->activity_end_at);
                $item->delete();
            }
        });
    }
}
